<template>
	<view class="add">
		<view style="padding: 15px;">
			<view class="par-iphone">
				<view class="iphone-text">联系人</view>
				<input type="text" v-model="name" placeholder="请输入姓名" />
			</view>
			<view class="par-iphone">
				<view class="iphone-text">手机号码</view>
				<input type="number" v-model="mobile" placeholder="请输入联系人电话" />
			</view>
			<view class="par-iphone" @tap="handleTap('picker3')">
				<view class="iphone-text">省市区</view>
				<input type="type" placeholder="点击按钮选择" v-model="addr" disabled/>
				<view class="add-but">选择地区</view>
			</view>
			<view class="par-iphone" @click="open()">
				<view class="iphone-text">所属街道</view>
				<input type="type" placeholder="点击获取" v-model="street" disabled/>
				<view class="add-but">选择街道</view>
			</view>
			<view class="par-iphone" @click="hanldMap()">
				<view class="iphone-text">回收地址</view>
				<input type="type" placeholder="点击获取" v-model="reAddress" />
				<view class="add-but">选择地址</view>
			</view>
			<view class="par-iphone">
				<view class="iphone-text">详细地址</view>
				<input type="text" v-model="address" placeholder="如1号楼1单元102" />
			</view>
			<view class="add-default">
				<view style="font-size: 14px;color: #343633;font-weight: bold;">设置为默认地址</view>
				<switch :checked="switchCheck" @change="switchChange" color="#3E9AFC" style="transform:scale(0.6)" />

			</view>

			<view class="par-but" v-if="type == 'add'" @click="save">保存</view>
			<view class="par-but" v-if="type == 'edit'" @click="save">修改</view>
		</view>
		<!-- <view class="add-del" @click="delAddress" v-if="type == 'edit'">删除地址</view> -->

		
		<!--省市区-->
		<lb-picker ref="picker3"
		  v-model="value3"
		  mode="multiSelector"
		  :list="list"
		  :level="3"
		  @confirm="handleConfirm">
		</lb-picker>


		<!--街道-->
		<uni-popup ref="recash2" type="bottom" style="z-index: 9999999999;">
			<view class="cash2">
				<view class="cash2-con">
					<view class="cash2-title" v-if="addr">当前城市：{{addr}}</view>
					<view class="cash2-tip" v-if="streetList.length >= 7">(温馨提示：下拉显示更多)</view>
					<scroll-view scroll-y show-scrollbar>
						<view class="cash2-list">
							<view class="list-item" v-for="(item,index) in streetList"  @click="confirmStreet(item)" :key="index">{{item.name}}</view>
						</view>
					</scroll-view>
				</view>

				<!-- <view class="cash-but3">
					<view class="but-left3">
						取消
					</view>
					<view class="but-right3">
						确定
					</view>
				</view> -->
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import LbPicker from '@/components/lb-picker'
	import areaData from 'libs/area-data-min'
	export default {
		data() {
			return {
				reAddress: "", //回收地区
				value3: [],
				list: areaData,
				
				name:'',
				mobile:wx.getStorageSync('wechatMobile'),
				address:'',
				street:'',
				streetId:'',
				streetList:[],
				addr:'',
				
				switchCheck:false,
				position:'',
				
				addressInfo:{},
				
				type:'add',
				
			}
		},
		onLoad(options) {
			if(options.address){
				this.addressInfo = JSON.parse(options.address);
				this.type = 'edit'
				if(this.addressInfo.is_default == 2){
					this.switchCheck = true
				}
				this.position = {
					longitude:this.addressInfo.lon,
					latitude:this.addressInfo.lat
				}
				this.reAddress = this.addressInfo.posaddress
				this.addr = this.addressInfo.province_text+this.addressInfo.city_text+this.addressInfo.area_text
				this.mobile = this.addressInfo.mobile
				this.name = this.addressInfo.name
				this.address = this.addressInfo.address
				this.street = this.addressInfo.street_text
				this.streetId = this.addressInfo.street
				this.value3 = [
					{
						label:this.addressInfo.province_text,
						value:this.addressInfo.province
					},
					{
						label:this.addressInfo.city_text,
						value:this.addressInfo.city
					},
					{
						label:this.addressInfo.area_text,
						value:this.addressInfo.area
					}
				]
			}
		},
		onShow(){
			if(getApp().globalData.reAddress){
				this.reAddress = getApp().globalData.reAddress;
			}
			if(getApp().globalData.position){
				this.position = JSON.parse(getApp().globalData.position)
			}
			if(!(this.addr && this.street)){
				this.getLocation()
			}
		},
		components: {
			LbPicker
		},
		methods: {
			getLocation(){
				let that = this
				uni.showLoading({})
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						uni.hideLoading({})
						//地址未变不拉取geocoder接口  接口有限制
						if (!(uni.getStorageSync('addr_longitude') == res.longitude && uni.getStorageSync('addr_latitude') == res.latitude)) {
							that.getgeocoder(res.longitude, res.latitude)
						} else {
							that.addr = uni.getStorageSync('address_province')+uni.getStorageSync('address_city')+uni.getStorageSync('address_district')
							that.street = uni.getStorageSync('street_title');
							that.streetId = uni.getStorageSync('street_id');
							if(uni.getStorageSync('value3')){
								that.value3 = JSON.parse(uni.getStorageSync('value3'))
							}
							if(!that.streetId){
								that.getgeocoder(res.longitude, res.latitude)
							}
							that.type = 'edit'
						}
						uni.setStorageSync('addr_longitude', res.longitude)
						uni.setStorageSync('addr_latitude', res.latitude)
					},
					fail(err){
						// wx.showModal({
						// 	content:"getLocation fail====" + JSON.stringify(err),
						// 	confirmText: "确认",
						// 	showCancel:false,
						// 	success: (res) => {
						// 		//点击“确认”时打开设置页面
						// 		if (res.confirm) {} else {}
						// 	}
						// });
						uni.hideLoading({})
						wx.getSetting({
						    success: (res)=>{
						        if(!res.authSetting['scope.userLocation']){
									wx.showModal({
										content: "检测到您没有授权定位，将无法下单，是否去设置打开？",
										confirmText: "确认",
										cancelText: "取消",
										success: (res) => {
											//点击“确认”时打开设置页面
											if (res.confirm) {
												wx.openSetting({
													success: (res) => {
														uni.getLocation({
															type: 'wgs84',
															success: function(res) {
																if (!(uni.getStorageSync('addr_longitude') == res.longitude && uni.getStorageSync('addr_latitude') == res.latitude)) {
																	that.getgeocoder(res.longitude, res.latitude)
																} else {
																	that.addr = uni.getStorageSync('address_province')+uni.getStorageSync('address_city')+uni.getStorageSync('address_district')
																	that.street = uni.getStorageSync('street_title');
																	that.streetId = uni.getStorageSync('street_id');
																	if(uni.getStorageSync('value3')){
																		that.value3 = JSON.parse(uni.getStorageSync('value3'))
																	}
																	if(!that.streetId){
																		that.getgeocoder(res.longitude, res.latitude)
																	}
																	that.type = 'edit'
																}
																uni.setStorageSync('addr_longitude', res.longitude)
																uni.setStorageSync('addr_latitude', res.latitude)
															},
														})
													}
												})
											} else {
												wx.showToast({
													title: '取消授权定位',
													icon: 'none'
												});
												console.log('用户点击取消')
											}
										}
									});
								}
						    }
						})
					}
				})
			},
			//经纬度获取区域信息
			getgeocoder(longitude, latitude) {
				let params = {
					lat: latitude,
					lng: longitude
				}
				this.$http.geocoder(params).then(res => {
					console.log(res)
					uni.setStorageSync('address_province', res.data.province)
					uni.setStorageSync('address_city', res.data.city)
					uni.setStorageSync('address_district', res.data.district)
					uni.setStorageSync('street_title', res.data.street_title)
					uni.setStorageSync('street_id', res.data.street)
					this.addr = res.data.province+res.data.city+res.data.district
					this.street = res.data.street_title;
					this.streetId = res.data.street
					this.value3 = [
						{
							label:res.data.province,
							value:res.data.nation_code
						},
						{
							label:res.data.city,
							value:res.data.city_code
						},
						{
							label:res.data.district,
							value:res.data.adcode
						}
					]
					uni.setStorageSync('value3', JSON.stringify(this.value3))
					this.type = 'edit'
				})
			},
			//删除地址
			delAddress(){
				this.$http.del_address({id:this.addressInfo.id}).then(res => {
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code == 0){ 
						setTimeout(() => {
							uni.redirectTo({
								url:'/pages/member/addressList'
							})
						},1000)
					}
				})
			},
			switchChange(e){
				this.switchCheck = e.detail.value
			},
			getStreet(id){
				this.$http.getstreet({id:id}).then(res => {
					console.log(res)
					this.streetList = res.data
				})
			},
			//确认选择街道
			confirmStreet(item){
				this.street = item.name
				this.streetId = item.id
				this.$refs.recash2.close()
			},
			//省市区
			handleTap (picker) {
				this.$refs[picker].show()
			},
			handleConfirm (item) {
				console.log('confirm::', item)
				this.value3 = item.item
				this.addr = this.value3[0].label+this.value3[1].label+this.value3[2].label
				this.getStreet(this.value3[2].value)
			},
			//街道
			open() {
				if(!this.addr){
					uni.showToast({
						title:'请先选择省市区',
						icon:'none'
					})
					return
				}
				if(this.value3.length>0 && this.type == 'edit'){
					this.getStreet(this.value3[2].value)
				}	
				this.$refs.recash2.open()
			},
			hanldMap() {
				uni.navigateTo({
					url: 'map'
				})
			},
			save(){
				if(!this.name){
					uni.showToast({
						title:'请输入姓名',
						icon:'none'
					})
					return
				}
				if(!this.mobile){
					uni.showToast({
						title:'请输入手机号码',
						icon:'none'
					})
					return
				}
				if(this.value3.length == 0){
					uni.showToast({
						title:'请选择省市区',
						icon:'none'
					})
					return
				}
				if(!this.street){
					uni.showToast({
						title:'请选择街道',
						icon:'none'
					})
					return
				}
				// if(!this.reAddress){
				// 	uni.showToast({
				// 		title:'请选择回收地址',
				// 		icon:'none'
				// 	})
				// 	return
				// }
				if(!this.address){
					uni.showToast({
						title:'请输入详细地址',
						icon:'none'
					})
					return
				}
				let province = this.value3[0].value
				let city = this.value3[1].value
				let area = this.value3[2].value
				let formData;
				if(this.type == 'edit'){
					formData = {
						id:this.addressInfo.id,
						mobile:this.mobile,
						name:this.name,
						positive:this.positive,
						negative:this.negative,
						// province:province,
						// city:city,
						posaddress:this.reAddress,
						area:area,
						street:this.streetId,
						address:this.address,
						lon:this.position?this.position.longitude:'',
						lat:this.position?this.position.latitude:'',
						is_default:this.switchCheck?2:1
					}
				}else{
					formData = {
						mobile:this.mobile,
						name:this.name,
						positive:this.positive,
						negative:this.negative,
						posaddress:this.reAddress,
						// province:province,
						// city:city,
						area:area,
						street:this.streetId,
						address:this.address,
						lon:this.position?this.position.longitude:'',
						lat:this.position?this.position.latitude:'',
						is_default:this.switchCheck?2:1
					}
				}
				
				this.$http.add_address(formData).then(res => {
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code == 0){ 
						//添加成功清空暂存数据
						getApp().globalData.reAddress = ''
						getApp().globalData.position = ''
						setTimeout(() => {
							// uni.navigateTo({
							// 	url:"/pages/member/addressList"
							// })
							uni.navigateBack({})
						},1000)
					}
				})
			},
		}
	}
</script>

<style scoped>
	.add {
		width: 100%;
	}

	.par-iphone:nth-of-type(1) {
		margin: 0;
	}

	.par-iphone {
		display: flex;
		flex-direction: row;
		background-color: rgba(187, 187, 187, 0.1);
		height: 43px;
		border-radius: 5px;
		align-items: center;
		margin: 15px 0;
		position: relative;
	}

	.iphone-text {
		color: #242424;
		font-size: 14px;
		padding: 0 12px;
		min-width: 116upx;
	}

	.uni-input-placeholder {
		font-size: 12px;
		color: #898989;
	}

	.add-but {
		border-radius: 15px;
		border: 1px solid #3E9AFC;
		color: #3E9AFC;
		padding: 3px 12px;
		font-weight: bold;
		position: absolute;
		right: 10px;
	}

	.par-but {
		width: 618upx;
		height: 80upx;
		
background: linear-gradient(124deg, #6AA7E8 0%, #3D9AFD 100%);
		
box-shadow: 0px 2px 4px 0px #1E75FF;
		border-radius: 48upx;
		line-height: 80upx;
		text-align: center;
		font-size: 14px;
		color: #FFFFFF;
		margin: 10px auto;
	}

	.add-default {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin: 20px 5px;
	}

	.add-del {
		color: #393D54;
		font-size: 16px;
		text-align: center;
		height: 48px;
		line-height: 48px;
		font-weight: bold;
		position: fixed;
		bottom: 0;
		background-color: #D3D4D9;
		width: 100%;
	}

	.add>>>.middle_view {
		border-radius: 0;
		width: 100vw;
	}

	.add>>>uni-button:after {
		display: none;
	}

	.add>>>uni-button {
		border-radius: 0;
		width: 100%;
	}

	.add>>>button {
		border-radius: 0;
		width: 100%;
	}

	.add>>>.flex_row {
		margin-top: 0 !important;
	}

	.add>>>.middle {
		/*  #ifdef  MP-WEIXIN  */
		top: 80%;
		/*  #endif  */

		/*  #ifdef  H5 */
		top: 82%;
		/*  #endif  */

	}

	/*街道*/
	.cash2 {
		height: 942upx;
		background: rgba(255, 255, 255, 1);
		border-radius: 25px 25px 0px 0;
	}

	.cash-but3 {
		display: flex;
		flex-direction: row;
		position: fixed;
		bottom: 0;
		justify-content: space-between;
		width: 100%;
	}

	.but-left3 {
		background-color: rgba(211, 212, 217, 1);
		width: 50%;
		height: 48px;
		text-align: center;
		font-size: 16px;
		color: rgba(57, 61, 84, 1);
		font-weight: bold;
		line-height: 48px;
	}

	.but-right3 {
		
background: linear-gradient(124deg, #6AA7E8 0%, #3D9AFD 100%);
		
box-shadow: 0px 2px 4px 0px #1E75FF;
		width: 50%;
		height: 48px;
		text-align: center;
		font-size: 16px;
		color: #fff;
		font-weight: bold;
		line-height: 48px;
	}

	.cash2-title {
		font-weight: bold;
		font-size: 16px;
		text-align: center;
		padding: 25px 0 10px 0;
	}
	.cash2-tip{
		text-align: center;
		font-size: 12px;
		color: #FE4744;
		padding-bottom:20px;
	}

	.cash2-list {
		display: flex;
		flex-direction: column;
		height: 370px;
		margin-bottom: 50px;
	}

	.list-item {
		color: #30344C;
		font-size: 14px;
		padding: 15px 0;
		border-bottom: 1px solid #D9D9DB;
		text-align: center;
	}

	.list-item:nth-of-type(1) {
		border-top: 1px solid #D9D9DB;
	}
</style>
